package com.software.levelupandroid.service;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TimePickerDialog;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.software.levelupandroid.MainActivity;
import com.software.levelupandroid.R;
import com.software.levelupandroid.taskCount.view.TaskCountActivity;
import com.software.levelupandroid.taskShow.TaskShowActivity;

import java.util.Calendar;

/**
 * 要跳转到此NotionService要intent.putExtra()三个传输数据
 * 分别是title，content，task_type,task_id
 * task_type任务类型（通知型、计时型）
 * 1：通知型，
 * 2：计时型，要额外穿入计时时间（time）
 * 3：系统任务
 */

public class NotionService extends Service {
    String taskJson;
    public NotionService() {
    }



    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("onStartCommand");
        //启动线程执行耗时任务
        new Thread(){
            @Override
            public void run() {
                sendNoti(intent);
            }
        }.start();
        return super.onStartCommand(intent, flags, startId);
    }

    private void countNum() {
        for(int i = 1; i < 20; i++){
            System.out.println(i);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void sendNoti(Intent intent) {
        final String CHANNEL_ID = "demo_channel_id";
        final String CHANNEL_NAME = "level up";
        final String CHANNEL_DESCRIPTION = "leve up notion";

        String title = intent.getStringExtra("title");
        String content = intent.getStringExtra("content");
        String type = intent.getStringExtra("task_type");


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 步骤1：告诉 Notification.Builder 我们需要的通知内容
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentTitle(title)
                .setContentText(content)
                .setWhen(System.currentTimeMillis());

        RemoteViews mRemoteViews = new RemoteViews(getPackageName(),R.layout.notification);

        mRemoteViews.setTextViewText(R.id.tv1, title);
        mRemoteViews.setTextViewText(R.id.tv, content);

        //进行任务的分类操作
        if(type.equals("1") || type.equals("3")){
            /** 通知型任务操作按钮*/
            /**延迟*/
            Intent clickIntent = new Intent(this,WidgetBroadcastReceiver.class);
            clickIntent.putExtra("title",title);
            clickIntent.putExtra("content",content);
            clickIntent.putExtra("task_type","1");
            clickIntent.setAction("alarmManage");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, clickIntent, 0);
            mRemoteViews.setOnClickPendingIntent(R.id.btn1, pendingIntent);




            /**通知性任务的完成*/
//            Intent buttonIntent =new Intent(NotionService.this, NotionService.class);
//            PendingIntent intent_btn = PendingIntent.getActivity(NotionService.this,2,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//            mRemoteViews.setOnClickPendingIntent(R.id.btn2,intent_btn);

        } else if(type.equals("2")){
            /**计时型任务操作*/
            mRemoteViews.setTextViewText(R.id.btn1, "开始");
            String time = intent.getStringExtra("time");
            mRemoteViews.setViewVisibility(R.id.btn2, View.GONE);

            //启动跳转
//            Intent appIntent = new Intent(NotionService.this, TaskCountActivity.class);
//            PendingIntent pIntent = PendingIntent.getActivity(NotionService.this, 1, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//            mRemoteViews.setOnClickPendingIntent(R.id.btn1, pIntent);


        }
        builder.setContent(mRemoteViews);


        // 步骤2：Android 8.0 新增了 NotificationChannel,这里需要做版本区分
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, "level up", NotificationManager.IMPORTANCE_HIGH);
            mChannel.setDescription(CHANNEL_DESCRIPTION);
            notificationManager.createNotificationChannel(mChannel);
            builder.setChannelId(CHANNEL_ID);
        }
        // 步骤3：拿到我们前面让 Notification.Builder 去帮忙构建的通知
        Notification notification = builder.build();
        notificationManager.notify(1, notification);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("onDestroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }


}